/*
----------------------------------------------------------------------------
Copyright (c) 2007 - 2009, Wangxing SHI	<swxlion@gmail.com>

All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

    * Redistributions of source code must retain the above
	  copyright notice, this list of conditions and the
	  following disclaimer.

    * Redistributions in binary form must reproduce the above
	  copyright notice, this list of conditions and the following
	  disclaimer in the documentation and/or other materials
	  provided with the distribution.

    * Neither the copyright holder's name nor the names of its
	  contributors may be used to endorse or promote products
	  derived from this software without specific prior written
	  permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
----------------------------------------------------------------------------
*/

/*--------------------------- BasalLogger.hpp -----------------------------*/
/*===========================================================================
FILE: BasalLogger.hpp
Author: Wangxing SHI (swxlion)
Version: 0.0.1
Date: 2009-06-06

File Description:
    Define the base class and interface for logger.

Dependencies:
    Platform: Platform Independent.

History:
    version        author        date       description
	----------------------------------------------------
	0.0.1          swxlion       09-06-06   Created the first version.
===========================================================================*/
#ifndef _LUNA_LOG__BASAL_LOGGER_H_
#define _LUNA_LOG__BASAL_LOGGER_H_
/*===============================================================================
INCLUDES AND VARIABLE DEFINITIONS
=============================================================================== */
#include <string>
#include <sstream>
#include "BasalRecorder.hpp"

/*===============================================================================
PROTOTYPES DECLARATION
=============================================================================== */
namespace LunaLog
{
	class BasalLogger;

	/*========================================
	  Log Reaper
	==========================================*/
	class LogReaper
	{
		friend std::ostream&	operator<< ( std::ostream &, LogReaper *);
		friend std::ostream&	operator<< ( std::ostream &, LogReaper &);
		friend std::wostream&	operator<< (std::wostream &, LogReaper *);
		friend std::wostream&	operator<< (std::wostream &, LogReaper &);

	private:
		BasalLogger		*pLogger;
		int				iLevel;

	public:
		LogReaper(BasalLogger *logger, int level): pLogger(logger), iLevel(level) {}
	};

	/*========================================
	  Basal Logger
	==========================================*/
	class BasalLogger
	{
	private:
		ASDL::TDLinkNode<BasalLogger *>			pLoggerList;
		ASDL::TDLinkNode<BasalRecorder *>		pRecorderList;

		//MemoryPool;

#ifdef _MSC_VER
		CRITICAL_SECTION		Critical;
		HANDLE					hSemaphore;
#else
		pthread_mutex_t			Mutex;
		pthread_cond_t			Condition;
#endif

		int			iDisabledLevel;
		bool		bIsSyncMode;
		bool		bIsInited;
		bool		bIsFrozen;

#ifdef _MSC_VER
		static DWORD WINAPI			LoggerDaemon(LPVOID lpParameter);
#else
		static void *				LoggerDaemon(LPVOID lpParameter);
#endif

	protected:
		// RecorderAll();
		// LoggerToAll();

	public:
		BasalLogger();
		virtual ~BasalLogger();

		bool			RegisterToLoggerPool(const char * szLoggerName);
		URESULT			SyncInit();
		URESULT			AsyncInit( ULONG ulInitNumber = LunaLog::Constant::MemoryPool::gc_ulInitNum,
									ULONG ulMaxNumber = LunaLog::Constant::MemoryPool::gc_ulMaxNum,
									ULONG ulPerIncNumber = LunaLog::Constant::MemoryPool::gc_ulIncNum,
									ULONG ulPerfactNumber = LunaLog::Constant::MemoryPool::gc_ulPerfectNum,
									bool bBlock = false );
		URESULT			Init( bool bIsSync,
									ULONG ulInitNumber = LunaLog::Constant::MemoryPool::gc_ulInitNum,
									ULONG ulMaxNumber = LunaLog::Constant::MemoryPool::gc_ulMaxNum,
									ULONG ulPerIncNumber = LunaLog::Constant::MemoryPool::gc_ulIncNum,
									ULONG ulPerfactNumber = LunaLog::Constant::MemoryPool::gc_ulPerfectNum,
									bool bBlock = false );
		URESULT			Free();
		void			Freeze();

		bool			IsInited();
		bool			IsSyncMode();
		bool			IsAsyncMode();
		void			SetDisabledLevel(int level);
		int				GetDisabledLevel();

		bool			GetMemoryPoolStatus( MemoryPoolStatus &strMPS );
		bool			GetMemoryPoolStatusSnapshot( MemoryPoolStatus &strMPS );

	public:
		void Append(BasalLogger *);
		void Append(BasalLogger &);
		void Remove(BasalLogger *);
		void Remove(BasalLogger &);

		void Append(BasalRecorder *);
		void Append(BasalRecorder &);
		void Remove(BasalRecorder *);
		void Remove(BasalRecorder &);

	public:
		LogReaper		FatalReaper;
		LogReaper		ErrorReaper;
		LogReaper		WarnReaper;
		LogReaper		InfoReaper;
		LogReaper		DebugReaper;

		virtual bool	Append(const char         * info, int level) = 0;
		virtual bool	Append(const wchar_t      * info, int level) = 0;
		virtual bool	Append(const std::string  * info, int level) = 0;
		virtual bool	Append(const std::string  & info, int level) = 0;
		virtual bool	Append(const std::wstring * info, int level) = 0;
		virtual bool	Append(const std::wstring & info, int level) = 0;

		bool	Fatal(const char         * info) { return Append(info, LunaLog::Constant::LogLevel::Fatal); }
		bool	Fatal(const wchar_t      * info) { return Append(info, LunaLog::Constant::LogLevel::Fatal); }
		bool	Fatal(const std::string  * info) { return Append(info, LunaLog::Constant::LogLevel::Fatal); }
		bool	Fatal(const std::string  & info) { return Append(info, LunaLog::Constant::LogLevel::Fatal); }
		bool	Fatal(const std::wstring * info) { return Append(info, LunaLog::Constant::LogLevel::Fatal); }
		bool	Fatal(const std::wstring & info) { return Append(info, LunaLog::Constant::LogLevel::Fatal); }

		bool	Error(const char         * info) { return Append(info, LunaLog::Constant::LogLevel::Error); }
		bool	Error(const wchar_t      * info) { return Append(info, LunaLog::Constant::LogLevel::Error); }
		bool	Error(const std::string  * info) { return Append(info, LunaLog::Constant::LogLevel::Error); }
		bool	Error(const std::string  & info) { return Append(info, LunaLog::Constant::LogLevel::Error); }
		bool	Error(const std::wstring * info) { return Append(info, LunaLog::Constant::LogLevel::Error); }
		bool	Error(const std::wstring & info) { return Append(info, LunaLog::Constant::LogLevel::Error); }

		bool	Warn(const char         * info) { return Append(info, LunaLog::Constant::LogLevel::Warn); }
		bool	Warn(const wchar_t      * info) { return Append(info, LunaLog::Constant::LogLevel::Warn); }
		bool	Warn(const std::string  * info) { return Append(info, LunaLog::Constant::LogLevel::Warn); }
		bool	Warn(const std::string  & info) { return Append(info, LunaLog::Constant::LogLevel::Warn); }
		bool	Warn(const std::wstring * info) { return Append(info, LunaLog::Constant::LogLevel::Warn); }
		bool	Warn(const std::wstring & info) { return Append(info, LunaLog::Constant::LogLevel::Warn); }

		bool	Info(const char         * info) { return Append(info, LunaLog::Constant::LogLevel::Info); }
		bool	Info(const wchar_t      * info) { return Append(info, LunaLog::Constant::LogLevel::Info); }
		bool	Info(const std::string  * info) { return Append(info, LunaLog::Constant::LogLevel::Info); }
		bool	Info(const std::string  & info) { return Append(info, LunaLog::Constant::LogLevel::Info); }
		bool	Info(const std::wstring * info) { return Append(info, LunaLog::Constant::LogLevel::Info); }
		bool	Info(const std::wstring & info) { return Append(info, LunaLog::Constant::LogLevel::Info); }

		bool	Debug(const char         * info) { return Append(info, LunaLog::Constant::LogLevel::Debug); }
		bool	Debug(const wchar_t      * info) { return Append(info, LunaLog::Constant::LogLevel::Debug); }
		bool	Debug(const std::string  * info) { return Append(info, LunaLog::Constant::LogLevel::Debug); }
		bool	Debug(const std::string  & info) { return Append(info, LunaLog::Constant::LogLevel::Debug); }
		bool	Debug(const std::wstring * info) { return Append(info, LunaLog::Constant::LogLevel::Debug); }
		bool	Debug(const std::wstring & info) { return Append(info, LunaLog::Constant::LogLevel::Debug); }
	};

	/*========================================
	  Overload Output Stream Operator
	==========================================*/
	std::ostream&	operator<< (std::ostream &, LogReaper   *);
	std::ostream&	operator<< (std::ostream &, LogReaper   &);
	std::ostream&	operator<< (std::ostream &, BasalLogger *);
	std::ostream&	operator<< (std::ostream &, BasalLogger &);

	std::wostream&	operator<< (std::wostream &, LogReaper   *);
	std::wostream&	operator<< (std::wostream &, LogReaper   &);
	std::wostream&	operator<< (std::wostream &, BasalLogger *);
	std::wostream&	operator<< (std::wostream &, BasalLogger &);
}

#endif // _LUNA_LOG__BASAL_LOGGER_H_
